from Map import *
from Definitions import *
ROOM_MAX_SIZE = 10
ROOM_MIN_SIZE = 6
MAX_ROOMS = 75
#generates a room and corridor layout with an easy algorithm
class Rect:
    #a rectangle
    def __init__(self, x, y, w, h):
        self.x1 = x
        self.y1 = y
        self.x2 = x+w
        self.y2 = y+h
    def center(self):#center of a rectangle
        center_x = (self.x1 + self.x2) / 2
        center_y = (self.y1 + self.y2) / 2
        return (center_x, center_y)
    def intersect(self, other):#returns true if two rectangles intersect
        return (self.x1 <= other.x2 and self.x2 >= other.x1 
                and self.y1 <= other.y2 and self.y2 >= other.y1)
class MapGenBasic(Map.Map):
    flags = (scrolling)
    def __init__(self, width, height):
        self.map = [['#'for i in range(width)] for j in range(height)]
        self.width = width
        self.height = height
        self.placepoints = []
    def initialize(self):#do the function
        rooms = []
        num_rooms = 0
        for r in range(MAX_ROOMS):
            #random width and height
            w = RNG.randint(ROOM_MIN_SIZE,ROOM_MAX_SIZE)
            h = RNG.randint(ROOM_MIN_SIZE,ROOM_MAX_SIZE)
            #random position
            x = RNG.randint(0,self.width-w-1)
            y = RNG.randint(0,self.height-h-1)
            #make the room
            new_room = Rect(x,y,w,h)
            
            #check if it intersects with other rooms
            failed = False
            for other_room in rooms:
                if new_room.intersect(other_room):
                    failed = True
                    break
            if not failed:
                #create the room
                self.create_room(new_room)
                #center coordinates
                (new_x, new_y) = new_room.center()
                if num_rooms == 0:
                    self.px = new_x#place the player
                    self.py = new_y
                else:#all rooms after the first are connected
                    self.placepoints.append((new_x, new_y))
                    (prev_x, prev_y) = rooms[num_rooms-1].center()
                    #draw two corridors
                    if (RNG.randint(0,1) == 0):
                        #go left/right, then up/down
                        self.create_h_tunnel(prev_x, new_x, prev_y)
                        self.create_v_tunnel(prev_y, new_y, new_x)
                    else:
                        #go up/down, then left/right
                        self.create_v_tunnel(prev_y, new_y, prev_x)
                        self.create_h_tunnel(prev_x, new_x, new_y)
                rooms.append(new_room)
                num_rooms += 1
    def aftermapscript(self):
        #move the player
        Game()[A_PLAYER].place(self.px,self.py)
        Game()[A_MAP][L_TRA].addObject(TeleportationTrap(),self.px-1,self.py-1,'^',L.violet)
        Game()[A_PLAYER].inventory.addItem(EnergyPulseWeapon())
        del self.px
        del self.py
        #add the loot and monsters
        for point in self.placepoints:
            gennum = RNG.randint(1,100)
            if (gennum <= 5):
                self.addItem(point[0],point[1],EnergyPulseWeapon())
            elif (gennum <= 40):
                self.addItem(point[0],point[1],ArmorTest())
            elif (gennum <= 90):
                tg = TreasureGuard(*point)
                if (RNG.randint(0,2) == 0):#timeguard may start with a machette
                    tg.inventory.addItem(Machette())
                    tg.inventory.equipItem(1)
                self.addCreature(tg)
            else:
                self.addCreature(Dragon(*point))
        del self.placepoints
    def create_room(self,room):#makes a rectangular region a floor
        for x in range(room.x1+1,room.x2):
            for y in range(room.y1+1,room.y2):
                self.map[y][x] = '.'#make it a floor
    def create_h_tunnel(self, x1, x2, y):#makes a horizantal tunnel
        for x in range(min(x1, x2), max(x1, x2) + 1):
            self.map[y][x] = '.'
    def create_v_tunnel(self, y1, y2, x):#makes a vertical tunnel
        for y in range(min(y1, y2), max(y1, y2) + 1):
            self.map[y][x] = '.'